<?php

/**
 * Class CommodityFavorite_Model
 */
class CommodityFavorite_Model extends CI_Model
{
    /**
     *
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * @param int $commodityId
     * @param int $userId
     * @return bool
     */
    public static function toggleCommodityFavorite($commodityId, $userId)
    {
        if (self::isCommodityFavoriteExist($commodityId, $userId)) {
            self::deleteCommodityFavorite($commodityId, $userId);
            return false;
        } else {
            self::addCommodityFavorite($commodityId, $userId);
            return true;
        }
    }

    /**
     * @param int $commodityId
     * @param int $userId
     * @return bool
     */
    public static function addCommodityFavorite($commodityId, $userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('commodity_favorite', array('commodity_id' => $commodityId, 'user_id' => $userId));

        if ($result)
            return true;
        else
            return false;
    }


    /**
     * @param int $commodityId
     * @param int $userId
     * @return bool
     */
    public static function deleteCommodityFavorite($commodityId, $userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('commodity_favorite.commodity_id', $commodityId);
        $CI->db->where('commodity_favorite.user_id', $userId);

        $result = $CI->db->delete('commodity_favorite');
        if ($result)
            return true;
        else
            return false;
    }

    /**
     * @param int $commodityId
     * @return mixed
     */
    public static function countCommodityFavoriteByCommodityId($commodityId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_favorite.id');
        $CI->db->from('commodity_favorite');
        $CI->db->where('commodity_favorite.commodity_id', $commodityId);

        return $CI->db->count_all_results();
    }

    /**
     * @param int $userId
     * @return mixed
     */
    public static function countCommodityFavoriteByUserId($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_favorite.id');
        $CI->db->from('commodity_favorite');
        $CI->db->where('commodity_favorite.user_id', $userId);

        return $CI->db->count_all_results();
    }
    public static function getCommodityFavArrayByUserId($userId, $limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_favorite.*');
        $CI->db->from('commodity_favorite');
        $CI->db->where('user_id', $userId);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query  = $CI->db->get();
        $result = $query->result();
            $array = array();
            foreach ($result as $row) {
                if(Commodity_Model::getCommodityById($row->commodity_id)!==null){
                    $array[] = Commodity_Model::getCommodityById($row->commodity_id);
                }
            }
            return $array;
        }

    /**
     * @param int $commodityId
     * @param int $userId
     * @return bool
     */
    public static function isCommodityFavoriteExist($commodityId, $userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_favorite.id');
        $CI->db->from('commodity_favorite');
        $CI->db->where('commodity_favorite.commodity_id', $commodityId);
        $CI->db->where('commodity_favorite.user_id', $userId);

        if ($CI->db->count_all_results())
            return true;
        else
            return false;
    }
}
